package com.yufei.entity.goods;

import java.io.BufferedReader;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yufei.utils.CommonUtil;
import com.yufei.utils.PatternUtils;

/**
 * @author jasstion
 * 上午9:44:12
 */
public class DefaultGoodsPropScoreGetter implements GoodsPropScoreGetter {
	private static Log mLog = LogFactory.getLog(DefaultGoodsPropScoreGetter.class);

	private static Map<String,String> propMap=new HashMap<>();
	private void initPropMap() throws IOException{
		if(propMap.size()==0){
			InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream("notebook_prop_values");
			BufferedReader br=new BufferedReader(new InputStreamReader(in));
			String line=null;
			do {
				line = br.readLine();
				if (line != null && !"".equals(line.trim())) {
					propMap.put(line.split(":")[0], line.split(":")[1]);
				}
			} while (line != null);
		}
	}

	@Override
	public int getScoreByPropKey(String propKey,String propValue) {
		
		// TODO Auto-generated method stub
		boolean isReverse=false;
		if(propKey.contains("-")){
			isReverse=true;
			propKey=propKey.replaceAll("-", "");
		}
		String propVales = propMap.get(propKey);
		if(CommonUtil.isEmptyOrNull(propVales)){
			mLog.info("配置文件中找不到此商品属性值"+propKey+"");
			return 0;
		}
		String[] propValues=propVales.split(",");
		int score=0;
		int total=100;
		int position=-1;
		int propValusSize=propValues.length;
		for(int i=0;i<propValues.length;i++){
			String tempValue=propValues[i];
			if(tempValue.contains("-")){
				//区间值
				float floatV=Float.parseFloat(propValue);
				float begf=Float.parseFloat(PatternUtils.getStrByRegex(tempValue, "(.*?)-"));
				float endf=Float.parseFloat(PatternUtils.getStrByRegex(tempValue, "-(.*)"));
                if(floatV>=begf&&floatV<=endf){
                	position=i;
                	break;
                }
				
				
			}
			else{
				if(tempValue.equals(propValue)){
					position=i;
					break;
					
				}
			}
			
		}
		if(position==-1){
			mLog.info("找不到配置文件中配置的关于"+propKey+"的值"+propValue+"");
		}
		if(isReverse){
			//权重根据序号递减
			position=propValusSize-position-1;
			score+=(100/propValusSize)*(position+1);
		}
		else{
			score+=(100/propValusSize)*(position+1);
		}
		return score;
	}
	public DefaultGoodsPropScoreGetter() {
		super();
		try {
			this.initPropMap();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("商品属性文件加载失误！");
		}
		// TODO Auto-generated constructor stub
	}
public static void main(String[] args) {
	DefaultGoodsPropScoreGetter dsg = new DefaultGoodsPropScoreGetter();
	System.out.print(dsg.getScoreByPropKey("-1screen", "11")+"\n");
    //玩游戏(cpu,screen,)
	mLog.info(dsg.getScoreByPropKey("cpu", "i7")+dsg.getScoreByPropKey("gc", "2")+"\n");
	//游戏机，价格低点
	mLog.info(dsg.getScoreByPropKey("cpu", "i7")+dsg.getScoreByPropKey("gc", "2"));//+dsg.getScoreByPropKey("-price", "4999")+"\n");
	mLog.info(dsg.getScoreByPropKey("cpu", "i5")+dsg.getScoreByPropKey("gc", "2"));//+dsg.getScoreByPropKey("-price", "2999")+"\n");
   //高性价比
	mLog.info(dsg.getScoreByPropKey("cpu", "i7")+dsg.getScoreByPropKey("gc", "2")+dsg.getScoreByPropKey("-price", "4999")+"\n");
	mLog.info(dsg.getScoreByPropKey("cpu", "i5")+dsg.getScoreByPropKey("gc", "2")+dsg.getScoreByPropKey("-price", "3600")+"\n");

}
}
